﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;
using System.Collections;

namespace ATW.MT.BUS
{
    public class bus_C_Conn
    {
        DataAccess da = null;
        public bus_C_Conn()
        {
            da = new DataAccess();
        }
        #region 方法
        /// <summary>
        /// 连接方式查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable ConnPagerQuery(string name, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select c.ConnID,s1.[Desc] DescZh,isnull(s2.[Desc],'暂无') DescEN,c.create_time from C_Connection c 
	                            left join(select * from C_Connection_MultiLan where LanId='zh' ) s1 on c.ConnID=s1.ConnID
	                            left join(select * from C_Connection_MultiLan where LanId='en' )s2 on c.ConnID=s2.ConnID");
            if (!String.IsNullOrEmpty(name))
            {
                sql.Append(" and s1.[desc] like '%" + name + "%'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "connid", "create_time desc");
        }
        /// <summary>
        /// 新增连接方式
        /// </summary>
        /// <param name="desc">中文</param>
        /// <returns></returns>
        public bool ConnAdd(string desc, string code)
        {
            string connid = getNewId();
            string main = "insert into [C_Connection](connid) values ('" + connid + "')";

            string sub = "insert into [C_Connection_MultiLan] (connid,lanid,[desc])values('" + connid + "','zh','" + desc + "')";
            string sub2 = "insert into [C_Connection_MultiLan] (connid,lanid,[desc])values('" + connid + "','en','" + code + "')";
            ArrayList al = new ArrayList();
            al.Add(main);
            al.Add(sub);
            al.Add(sub2);
            return da.ExecuteArrayList(al);
        }
        /// <summary>
        /// 连接方式是否存在
        /// </summary>
        /// <param name="desc"></param>
        /// <returns></returns>
        public bool IsConnExist(string desc)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from C_Connection_MultiLan where [Desc]='{0}' and lanid='zh'", desc);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 连接方式描述是否存在
        /// </summary>
        /// <param name="connid"></param>
        /// <param name="lanid"></param>
        /// <returns></returns>
        public bool IsConnDescExist(string connid, string lanid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from C_Connection_MultiLan where connid='{0}' and lanid='{1}'", connid, lanid);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 修改描述
        /// </summary>
        /// <param name="connid"></param>
        /// <param name="desc">描述</param>
        /// <param name="code">缩写</param>
        /// <returns></returns>
        public bool ConnDescEdit(string connid, string desc, string code)
        {
            ArrayList al = new ArrayList();
            string s1 = "Update C_Connection_MultiLan set [desc]='" + desc + "' where connid='" + connid + "' and lanid='zh'";
            string s2 = "Update C_Connection_MultiLan set [desc]='" + code + "' where connid='" + connid + "' and lanid='en'";
            al.Add(s1);
            al.Add(s2);
            return da.ExecuteArrayList(al);
        }
        /// <summary>
        /// 连接方式描述查询
        /// </summary>
        /// <param name="connid"></param>
        /// <returns></returns>
        public DataTable ConnDescQuery(string connid)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select cm.*,l.LanName from C_Connection_MultiLan cm left join D_Language l on cm.LanId=l.LanId 
                            where cm.connid='" + connid + "'");
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增描述
        /// </summary>
        /// <param name="connid"></param>
        /// <param name="lanid"></param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        public int ConnDescAdd(string connid, string lanid, string Desc)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into C_Connection_MultiLan (connid,lanid,[desc]) values('{0}','{1}','{2}')", connid, lanid, Desc);
            return da.ExcuteNoQuery(sql.ToString());
        }
        public string getNewId()
        {
            string sql = "select newid()";
            object o = da.ExecuteScalar(sql);
            if (o != null && o.ToString() != "")
            {
                return o.ToString();
            }
            return "";
        }
        #endregion
    }
}